package com.ztz.mynewdemo.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.ztz.mynewdemo.MainActivity;
import com.ztz.mynewdemo.R;
import com.ztz.mynewdemo.utils.AppConfigUtils;

import java.util.Random;

public class WelcomeActivity extends BaseActivity {
    private Handler handler;
    int[] images;
    private ImageView iv_bg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_welcome);
        findViews();
        randomIndex();
        animateImage();
        handler=new Handler();
    }
    public static final float SCALE_SIZE=1.3F;
    private void animateImage() {
        ObjectAnimator animatorX=ObjectAnimator.ofFloat(iv_bg,"scaleX",1f,SCALE_SIZE);
        ObjectAnimator animatorY=ObjectAnimator.ofFloat(iv_bg,"scaleY",1f,SCALE_SIZE);
        AnimatorSet set=new AnimatorSet();
        set.setDuration(2000).play(animatorX).with(animatorY);
        set.start();
        //对动画进行监听
        //动画结束再跳转
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                boolean guide= AppConfigUtils.getInstance().getLoginStatu(WelcomeActivity.this);
                if(guide)
                {
                    startActivity(new Intent(WelcomeActivity.this,LoginActivity.class));
                }
                else
                {
                    startActivity(new Intent(WelcomeActivity.this,MainActivity.class));
                }
                finish();
            }
        });
    }

    private void randomIndex() {
        images=new int[]{R.mipmap.read1,R.mipmap.read4,R.mipmap.read5,R.mipmap.read6,R.mipmap.read7};
        Random random=new Random();
        int index=random.nextInt(images.length);
        iv_bg.setImageResource(images[index]);
    }

    private void findViews() {
        iv_bg=(ImageView)findViewById(R.id.iv_welcome);
        TextView tv_version=(TextView)findViewById(R.id.tv_version);
        tv_version.setText(getVersion()+"");
    }
    private  String getVersion()
    {
        try
        {
            PackageManager manager=getPackageManager();
            PackageInfo info=manager.getPackageInfo(getPackageName(),0);
            String versionName=info.versionName;
            return versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "1.0";
        }
    }
//    Runnable runnable=new Runnable() {
//        @Override
//        public void run() {
//            boolean guide= AppConfigUtils.getInstance().getLoginStatu(WelcomeActivity.this);
//            if(guide)
//            {
//                startActivity(new Intent(WelcomeActivity.this,LoginActivity.class));
//            }
//            else
//            {
//                startActivity(new Intent(WelcomeActivity.this,MainActivity.class));
//            }
//            finish();
//        }
//    };
//    @Override
//    protected void onPause() {
//        super.onPause();
//        handler.removeCallbacks(runnable);
//    }
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        handler.removeCallbacks(runnable);
//    }
}
